package com.kagu.wms.manifest.httprov;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.kagu.wms.module.KaguModule;
import com.wowza.io.DirectRandomAccessReader;
import com.wowza.io.HTTPDirectRandomAccessReader;
import com.wowza.io.IRandomAccessReader;
import com.wowza.wms.http.HTTProvider2Base;
import com.wowza.wms.http.IHTTPRequest;
import com.wowza.wms.http.IHTTPResponse;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.module.ModuleBase;
import com.wowza.wms.module.ModuleConnect;
import com.wowza.wms.module.ModuleCore;
import com.wowza.wms.module.ModuleProperties;
import com.wowza.wms.stream.MediaStreamBase;
import com.wowza.wms.util.ModuleUtils;
import com.wowza.wms.vhost.IVHost;
import com.wowza.wms.vhost.VHostSingleton;

public class AdobeManifestHTTProvider extends HTTProvider2Base {

	private StringBuilder content;

	@Override
	public void init() {
		// TODO Auto-generated method stub
		super.init();
		String homePath = VHostSingleton.getInstance(
				(String) VHostSingleton.getVHostNames().get(0)).getHomePath();

		System.err.println("home path: " + homePath);
		content = new StringBuilder();
		String sep = File.separator;
		try {
			BufferedReader br = new BufferedReader(new FileReader(homePath
					+ sep + "content" + sep + "am.f4m"));
			try {
				String line = null; // not declared within while loop
				while ((line = br.readLine()) != null) {
					content.append(line);
					content.append(System.getProperty("line.separator"));
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.err.println("##################################");
		System.err.println(content);
		System.err.println("##################################");
	}

	public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
		if (!doHTTPAuthentication(vhost, req, resp))
			return;
		// String retStr = "";
		// retStr = (new StringBuilder(String.valueOf(retStr))).append(
		// "<manifest xmlns=\"http://ns.adobe.com/f4m/2.0\">").toString();
		// retStr = (new StringBuilder(String.valueOf(retStr))).append(
		// "<baseURL>http://localhost:1935/vod/</baseURL>").toString();
		// retStr = (new StringBuilder(String.valueOf(retStr)))
		// .append(
		// "<media href=\"mp4:sample1_500kbps.f4v/manifest.f4m\" bitrate=\"500\"/>")
		// .toString();
		// retStr = (new StringBuilder(String.valueOf(retStr)))
		// .append(
		// "<media href=\"mp4:sample1_150kbps.f4v/manifest.f4m\" bitrate=\"150\"/>")
		// .toString();
		// retStr = (new StringBuilder(String.valueOf(retStr)))
		// .append(
		// "<media href=\"mp4:sample1_700kbps.f4v/manifest.f4m\" bitrate=\"700\"/>")
		// .toString();
		// retStr = (new StringBuilder(String.valueOf(retStr)))
		// .append(
		// "<media href=\"mp4:bigbuckbunny_1920.f4v/manifest.f4m\" bitrate=\"192\" type=\"audio\" label=\"Espanol\" lang=\"es\" alternate=\"true\"/>")
		// .toString();
		// retStr = (new StringBuilder(String.valueOf(retStr))).append(
		// "</manifest>").toString();

		System.err.println("!!!");
		try {
			resp.setHeader("Content-Type", "text/xml");
			OutputStream out = resp.getOutputStream();
			byte outBytes[] = content.toString().getBytes();
			out.write(outBytes);
		} catch (Exception e) {
			WMSLogger.getLogger("debug").error(
					"AdobeManifestHTTProvider: " + e.toString());
		}

	}

}
